<?php

class BatchHelper extends AppHelper {

	function total($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM runs r, runshasmacs rhm WHERE r.id = rhm.run_id AND r.id = '$id'");
		return mysql_fetch_object($result)->count;	
	}

	
	function open($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM macs AS m, runshasmacs AS rhm WHERE m.id = rhm.mac_id AND status_id NOT IN (25,27) AND rhm.run_id = '$id'");
		return mysql_fetch_object($result)->count;	
	}	
	
	
	function skipped($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM macs AS m, runshasmacs AS rhm WHERE m.id = rhm.mac_id AND status_id = 27 AND rhm.run_id = '$id'");
		return mysql_fetch_object($result)->count;		
	}
		
	function completed($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM macs AS m, runshasmacs AS rhm WHERE m.id = rhm.mac_id AND status_id IN (25) AND rhm.run_id = '$id'");
		return mysql_fetch_object($result)->count;		
	}
	
	
	function batchstatus($id) {
		$result = mysql_query("SELECT COUNT(*)AS count FROM runs r, runshasstatusruns rhs WHERE r.id = rhs.run_id AND rhs.status_id = '22' AND r.id = '$id'");	
		if(mysql_fetch_object($result)->count == 0) {
			return "Started";
		}
		else {
			return "Completed";
		}
	}
	
	
	function currentstatus($id) {
		if($id == 23) { $nextstep = 23; }
		elseif($id == 24) { $nextstep = 24;  }
		elseif($id == 25) { $nextstep = 25;  }
		else { $nextstep = $id+1; }
		if($nextstep != 25) {
			$result = mysql_query("SELECT * FROM statuses WHERE statuses.order = '$nextstep'");
			return mysql_fetch_object($result)->statusdetail;
		}
		else {
			return "";
		}
	}
	
	
	function startdatum($id) {
		$result = mysql_query("SELECT DATE_FORMAT(date,'%d.%m.%Y') AS datum FROM runs r, runshasstatusruns rhs WHERE r.id = rhs.run_id AND rhs.status_id = '1' AND r.id = '$id'");	
		if(mysql_num_rows($result) > 0) {
			$date = mysql_fetch_object($result)->datum;
		}	
		else {
			$date = "-";
		}
		return $date;
	}
	
	
	function blacklisted($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM runs r,runshasmacs rhm, blacklists bl WHERE r.id = rhm.run_id AND rhm.mac_id = bl.mac_id AND r.id = '$id'");
		return mysql_fetch_object($result)->count;	
	}
	
	function blacklistedopen($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM runs r,runshasmacs rhm, blacklists bl WHERE r.id = rhm.run_id AND rhm.mac_id = bl.mac_id AND r.id = '$id' AND bl.status_id = '28'");
		return mysql_fetch_object($result)->count;	
	}
	
	
	function exceptions($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM runs r,runshasmacs rhm, blacklists bl WHERE r.id = rhm.run_id AND rhm.mac_id = bl.mac_id AND bl.problem_id = '10' AND r.id = '$id'");
		return mysql_fetch_object($result)->count;		
	}
	
	
	function ponds($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM pondcases AS pc, partrequests AS pr, runshasmacs AS rhm WHERE pc.partrequest_id = pr.id AND pr.mac_id = rhm.mac_id AND rhm.run_id = '$id'");
		return mysql_fetch_object($result)->count;		
	}
	
	
	function pondsopen($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM pondcases AS pc, partrequests AS pr, runshasmacs AS rhm WHERE pc.partrequest_id = pr.id AND pr.mac_id = rhm.mac_id AND pc.status_id IN (33,35) AND rhm.run_id = '$id'");
		return mysql_fetch_object($result)->count;		
	}
	
	function mmtotal($id){
		$result = mysql_query("SELECT COUNT(*) AS count FROM runs r, runshasmacs rhm, manualmoves mm WHERE r.id = rhm.run_id AND rhm.mac_id = mm.mac_id AND r.id = '$id'");
		return mysql_fetch_object($result)->count;	
	}
	
	function interactionstotal($id){
		$result = mysql_query("SELECT COUNT(*) AS count FROM runs r, runshasmacs rhm, customershasmacs chm, customers c, customerinteractions ci WHERE r.id = '$id' AND r.id = rhm.run_id AND rhm.mac_id = chm.mac_id AND chm.customer_id = c.id AND c.account_number = ci.x_accno");
		return mysql_fetch_object($result)->count;	
	}

	
}

?>